<?php
//link den smarty class
require_once('include/sessionclass.php');
session_start();
require_once('libs/Smarty.class.php');
require_once('include/oracle.php');
require_once('include/validation_class.php');
require_once('include/config_color.php');
require_once('include/ngonngu.php');
require_once('include/login.php');

//$USERID = '00000';
$smarty = new Smarty();

/****************************************************/
$TCVN=array('¸','µ','¶','·','¹','¨','¾','»','¼','½','Æ', '©','Ê','Ç','È','É','Ë', '®','Ð','Ì','Î','Ï','Ñ', 'ª','Õ','Ò','Ó','Ô','Ö', 'Ý','×','Ø','Ü','Þ', 'ã','ß','á','â','ä', '«','è','å','æ','ç','é', '¬','í','ê','ë','ì','î', 'ó','ï','ñ','ò','ô', '­','ø','õ','ö','÷','ù', 'ý','ú','û','ü','þ', '¡','¢','§','£','¤','¥','¦','A¸','Aµ','A¶','A·','A¹','¡¾','¡»','¡¼','¡½','¡Æ','¢Ê','¢Ç','¢È','¢É','¢Ë','EÐ','EÌ','EÎ','EÏ','EÑ','£Õ','£Ò','£Ó','£Ô','£Ö','IÝ','I×','IØ','IÜ','IÞ','Oã','Oß','Oá','Oâ','Oä','¤è','¤å','¤æ','¤ç','¤é','¥í','¥ê','¥ë','¥ì','¥î','Uó','Uï','Uñ','Uò','Uô','¦ø','¦õ','¦ö','¦÷','¦ù','Yý','Yú','Yû','Yü','Yþ');
/******************************************************/
$smarty->template_dir = 'smarty/templates';
$smarty->compile_dir = 'smarty/templates_c';
$smarty->cache_dir = 'smarty/cache';
$smarty->config_dir = 'smarty/configs';
/********ENLIGH - VN*******/
$smarty->assign('lbl_chinhanh', $VN_user[BRNCODE]);
$smarty->assign('lbl_maphonggd', $VN_user[SUBBRNCODE]);
$smarty->assign('lbl_mangdung', $VN_user[USRID]);
$smarty->assign('lbl_manv', $VN_user[EMPNO]);
$smarty->assign('lbl_tenngdung', $VN_user[USRNAME]);
$smarty->assign('lbl_hanmuc', $VN_user[LMTID]);
$smarty->assign('lbl_position', $VN_user[LEVELID]);

$smarty->assign('lbl_maphong', $VN_user[DEPTCD]);
//$smarty->assign('lbl_busy', $VN_user[busy]);
$smarty->assign('lbl_vantay', $VN_user[vantay]);
//$smarty->assign('lbl_Status_user', $VN_user[userstatus]);
$smarty->assign('lbl_ENABLED', $VN_user[ENABLED]);
$smarty->assign('lbl_IPADDRESS', $VN_user[IPADDRESS]);

$smarty->assign('lbl_Status', $VN_template[STATUS]);
$smarty->assign('ten_cap_nhat', $VN_template[UPDATE]);
$smarty->assign('ten_thuc_hien', $VN_template[ACTION]);
$smarty->assign('ten_tim_kiem', $VN_template[SEARCH]);
$smarty->assign('ten_themmoi', $VN_template[ADDNEW]);
$smarty->assign('ten_chitiet', $VN_template[DETAIL]);
$smarty->assign('ten_baotri', $VN_template[MAINTAIN]);
$smarty->assign('ten_trove', $VN_template[BACK]);
$smarty->assign('ten_chon', $VN_template[CHOOSE]);

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$javascript ='<link href="style/style.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/javascript" src="js/tongquat.js"></script>
<script language="JavaScript" type="text/javascript" src="js/user_id.js"></script>
<META HTTP-EQUIV="Refresh" CONTENT="' . $GLOBALS['conf_time']*60 .';URL=http://' . $host . $uri . '/blank.php?log=logout">
';
$smarty->assign('javascript', $javascript);
/**********************************************************************/

$txt_find = $_REQUEST['name_find'];
$find = $_REQUEST['find'];

$id = $_GET['id'];
$num_records = 10;
if($id != $find){
	$start_record = 1;
	$end_record = $num_records;
}else{
	$start_record = $_GET['start'] ; //Lay gia tri record bat dau hien tai
	$end_record = $_GET['end'];
}
if(!isset($txt_find)){
	$NOIDUNG = "";	
}else{
	$NOIDUNG = $txt_find;
}

if(!isset($find)){
	$TIEUCHI= '4';
}else{
	$TIEUCHI= $find;
}

$get_brncode=$_GET['brncode'];

if(!empty($get_brncode)){
	$NOIDUNG = $get_brncode;
	$TIEUCHI = '1';	
}

$smarty->assign('txt_find', $NOIDUNG);
$smarty->assign('activecbo', $TIEUCHI);
$NOIDUNG= str_replace($UNI_VN,$TCVN,$NOIDUNG);
$strquery = "$NOIDUNG|$TIEUCHI|*EOF|";
//Lay tong so record ve
$total_records = get_function(fun_getUserTotal,$strquery);

if($total_records <= $num_records){
	$smarty->assign('activeleft', 'disabled');
	$smarty->assign('activeright', 'disabled');
	$total_page = 1;
	$start_record = 1;
	$end_record = $num_records;
}else{//Neu so record nhieu hon num_records
	$smarty->assign('activeleft', 'disabled');//khoa nut ben trai
	$smarty->assign('activeright', 'enabled');//mo nut ben phai
	if(isset($_POST['search'])){
		$start_record = 1;
		$end_record = $num_records;
	}
	if(empty($start_record)){
		$start_record = 1;
		$end_record = $num_records;
	}
	if(isset($_POST['right'])){
		$smarty->assign('activeleft', 'enabled');
		$start_record = $start_record + $num_records;
		$end_record = $end_record + $num_records;
		if($end_record >= $total_records){
			$smarty->assign('activeright', 'disabled');
			$end_record = $total_records;
		}
	}
	
	if(isset($_POST['forward'])){
		$smarty->assign('activeleft', 'enabled');
		$smarty->assign('activeright', 'disabled');
		//trinh bay trang cuoi cung
		$num_page = ceil($total_records/$num_records);
		$start_record = ($num_page - 1) * $num_records + 1;
		$end_record = $total_records;
	}
	
	if(isset($_POST['left'])){
		$smarty->assign('activeright', 'enabled');
		if($end_record - $start_record >= $num_records){
			$start_record = $start_record - $num_records;
			$end_record = $end_record - $num_records;
		}else{//truong hop nguoi dung dang xem trang cuoi roi nhan nut left
			$end_record = $start_record - 1;
			$start_record = $end_record - $num_records + 1;
		}
		if($start_record <= 1){
			$smarty->assign('activeleft', 'disabled');
			$start_record = 1;
		}else{
			$smarty->assign('activeleft', 'enabled');
		}
	}
	
	if(isset($_POST['backward'])){
		$smarty->assign('activeleft', 'disabled');
		$smarty->assign('activeright', 'enabled');
		$start_record = 1;
		$end_record = $num_records;
	}
}
$smarty->assign('id', $TIEUCHI);
if($total_records != 0){
	$smarty->assign('start', $start_record);
}else{
	$smarty->assign('start', '0');
}
if($total_records <= $num_records){
	$smarty->assign('end', $total_records);
}else{
	$smarty->assign('end', $end_record);
}

$MDLNAME= "CM18203_UsrGenInq";
$strquery_fun = "$USERID|$MDLNAME|*EOF|";

$p_msg = get_function(fun_getUserRights,$strquery_fun);
/*NO : ko du quyen
OK: co quyen thuc hien
CM003: pro chua duoc dang ky trong bang par_sstxnlist*/
//$result = split_string($p_msg,"|");
$result =  $p_msg;
if($result=="NO"){
	$error=$VN_Right[NO];
}elseif($result=="CM003"){
	$error=$VN_Right[ERR];
}else{
	$strquery = "$USERID|$NOIDUNG|$TIEUCHI|$start_record|$end_record|*EOF|";
	$result = get_procedure_3($MDLNAME,$strquery);
	$i = 0;
	while($row = oci_fetch_array ($result,(OCI_NUM+OCI_RETURN_NULLS))){
		$j = 0;
		
		/*if(fmod($i,2)==0){
			$grid = '<tr class="odd">';
		}else{
			$grid = '<tr class="even">';
		}*/
		$grid = '<tr>';
		$grid = $grid . '<td align="center" class="even"><input name="radioTK" type="radio" value="' . $row[2] .'-'.$row[0].'"></td>';
		foreach($row as $col_value){
			if(isset($col_value)){
				$str[$j] = str_replace($TCVN,$UNICODE,$col_value);
			}else{
				$str[$j] = "&nbsp;";
			}
			if($j<8){//ko lay cot cuoi cung, do la cot so thu tu
				if(fmod($j,2)==0){
					$grid = $grid . '<td class="odd"';
				}else{
					$grid = $grid . '<td class="even"';
				}
				if($j!=3){//ko center cot ten tai khoan					
					$grid = $grid . ' align="center">' .  $str[$j] . '</td>';					
				}else{
					$grid = $grid . '>' . $str[$j] . '</td>';
				}
			}
			//$grid = $grid . '<td>' . $str[$j] . '</td>';
			$j++;
		}
		$grid = $grid . '</tr>';
		$value[$i] = $grid;
		$i++;
	}
	$smarty->assign('value', $value);
}

if(isset($_POST['addnew'])){
	$host  = $_SERVER['HTTP_HOST'];
	$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	redirect("http://$host$uri/them_moi_user.php");
	//exit();
}

if(isset($_POST['detail'])){
	$host  = $_SERVER['HTTP_HOST'];
	$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	$id = $_REQUEST['radioTK'];
	
	if(isset($id)){
		redirect("http://$host$uri/chi_tiet_user.php?id=$id");
	}
	
	//exit();
}
if(isset($_POST['maintain'])){
	$host  = $_SERVER['HTTP_HOST'];
	$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	$id = $_REQUEST['radioTK'];
	if(isset($id)){
		redirect("http://$host$uri/bao_tri_user.php?id=$id");
	}
}
if(isset($_POST['chuyentrang']) || isset($_POST['chuyentrang1'])){

	$action = $_REQUEST['hanhdong'];
	$id = $_REQUEST['radioTK'];
	$host  = $_SERVER['HTTP_HOST'];
	$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	switch ($action){
		case '1':
			redirect("http://$host$uri/user.php");
			break;
		case '2'://trang xem danh sach can duyet
			redirect("http://$host$uri/user_can_duyet.php");
			break;
		case '3':
			redirect("http://$host$uri/them_moi_user.php");
			break;
		case '4':
			redirect("http://$host$uri/chi_tiet_user.php?id=$id");
			break;
		case '5':
			redirect("http://$host$uri/bao_tri_user.php?id=$id");
			break;
		case '6':
			redirect("http://$host$uri/duyet_user.php?id=$id");			
			break;
	}

}

/*********************************/
$cbovalue = array(1 => $cbo_vn_user[1], 2 => $cbo_vn_user[2], 3 => $cbo_vn_user[3], 4=>$cbo_vn_user[4]);
$chuyentrang = array(1 => $cbo_chuyentrang[1] ,2 => $cbo_chuyentrang[2],3 => $cbo_chuyentrang[3],4 => $cbo_chuyentrang[4],5 => $cbo_chuyentrang[5],6 => $cbo_chuyentrang[6]);
$smarty->assign('chuyentrang', $chuyentrang);
$smarty->assign('select_cbo_chuyentrang',1);
$smarty->assign('cbovalue', $cbovalue);

if(isset($error))
	$smarty->assign('error', $error);
else
	$smarty->assign('error', "");
/**********************/
$smarty->display('user_id.tpl');
?>